package com.example.aicooking.controller;

import com.example.aicooking.entity.Works;
import com.example.aicooking.entity.vo.CookingStepVO;
import com.example.aicooking.entity.vo.WorksListVO;
import com.example.aicooking.service.CookingStepService;
import com.example.aicooking.service.WorksService;
import com.example.aicooking.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/works")
public class WorksController {
    @Autowired
    private WorksService worksService;

    @Autowired
    private CookingStepService cookingStepService;

    /**
     * 获取作品列表
     */
    @GetMapping("/list")
    public Result<List<WorksListVO>> getWorksList() {

        try {
            List<WorksListVO> worksList = worksService.getWorksList();
            return Result.success(worksList);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取作品详情
     */
    @GetMapping("/detail/{id}")
    public Result<Works> getWorksDetail(@PathVariable("id") Integer worksId) {
        try {
            Works works = worksService.getWorksDetail(worksId);
            return Result.success(works);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 新增的搜索作品接口
    @GetMapping("/works/search")
    public Result<List<WorksListVO>> searchWorks(@RequestParam String keyword) {
        List<WorksListVO> worksList = WorksService.searchWorksByKeyword(keyword);
        return new Result<>(1, "查询成功", worksList);
    }

    /**
     * 添加作品
     */
    @PostMapping("/add")
    public Result<Works> addWorks(@RequestBody Works works) {
        try {
            Works savedWorks = worksService.addWorks(works);
            return Result.success(savedWorks);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发布/取消发布作品
     */
    @PostMapping("/publish/{id}")
    public Result<String> publishWorks(
            @PathVariable("id") Integer worksId,
            @RequestParam Integer userId,
            @RequestParam Integer status) {
        try {
            worksService.toggleWorksStatus(worksId, userId, status);
            return Result.success(status == 1 ? "发布成功" : "取消发布成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除作品
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteWorks(
            @PathVariable("id") Integer worksId,
            @RequestParam Integer userId) {
        try {
            worksService.deleteWorks(worksId, userId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户作品列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<Works>> getUserWorks(@PathVariable Integer userId) {
        try {
            List<Works> works = worksService.getUserWorks(userId);
            return Result.success(works);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取作品的烹饪步骤
     */
    @GetMapping("/{worksId}/steps")
    public Result<List<CookingStepVO>> getSteps(@PathVariable Long worksId) {
        try {
            List<CookingStepVO> steps = cookingStepService.getStepsByWorksId(worksId);
            return Result.success(steps);
        } catch (Exception e) {
            return Result.error("获取烹饪步骤失败");
        }
    }

    /**
     * 保存作品的烹饪步骤
     */
    @PostMapping("/{worksId}/steps")
    public Result<List<CookingStepVO>> saveSteps(@PathVariable Long worksId,
                                  @RequestBody List<CookingStepVO> steps) {
        try {
            List<CookingStepVO> updatedSteps = cookingStepService.getStepsByWorksId(worksId);
            return Result.success(updatedSteps);
        } catch (Exception e) {
            return Result.error("保存烹饪步骤失败");
        }
    }

}
